﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:mrdepartment
	/// </summary>
	public partial class mrdepartment
	{
		public mrdepartment()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("DepID", "mrdepartment"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int DepID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from mrdepartment");
			strSql.Append(" where DepID=@DepID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@DepID", MySqlDbType.Int32)
			};
			parameters[0].Value = DepID;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.mrdepartment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into mrdepartment(");
			strSql.Append("BranchID,DepName,Manager,Tel1,Tel2,Fax,Province,City,District,Street,PostCode,DelFlag,ParentId,depcode)");
			strSql.Append(" values (");
			strSql.Append("@BranchID,@DepName,@Manager,@Tel1,@Tel2,@Fax,@Province,@City,@District,@Street,@PostCode,@DelFlag,@ParentId,@depcode)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BranchID", MySqlDbType.Int32,11),
					new MySqlParameter("@DepName", MySqlDbType.VarChar,20),
					new MySqlParameter("@Manager", MySqlDbType.VarChar,20),
					new MySqlParameter("@Tel1", MySqlDbType.VarChar,20),
					new MySqlParameter("@Tel2", MySqlDbType.VarChar,20),
					new MySqlParameter("@Fax", MySqlDbType.VarChar,20),
					new MySqlParameter("@Province", MySqlDbType.VarChar,20),
					new MySqlParameter("@City", MySqlDbType.VarChar,20),
					new MySqlParameter("@District", MySqlDbType.VarChar,20),
					new MySqlParameter("@Street", MySqlDbType.VarChar,20),
					new MySqlParameter("@PostCode", MySqlDbType.VarChar,20),
					new MySqlParameter("@DelFlag", MySqlDbType.Int32,4),
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@depcode", MySqlDbType.VarChar,50)};
			parameters[0].Value = model.BranchID;
			parameters[1].Value = model.DepName;
			parameters[2].Value = model.Manager;
			parameters[3].Value = model.Tel1;
			parameters[4].Value = model.Tel2;
			parameters[5].Value = model.Fax;
			parameters[6].Value = model.Province;
			parameters[7].Value = model.City;
			parameters[8].Value = model.District;
			parameters[9].Value = model.Street;
			parameters[10].Value = model.PostCode;
			parameters[11].Value = model.DelFlag;
			parameters[12].Value = model.ParentId;
			parameters[13].Value = model.depcode;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.mrdepartment model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update mrdepartment set ");
			strSql.Append("BranchID=@BranchID,");
			strSql.Append("DepName=@DepName,");
			strSql.Append("Manager=@Manager,");
			strSql.Append("Tel1=@Tel1,");
			strSql.Append("Tel2=@Tel2,");
			strSql.Append("Fax=@Fax,");
			strSql.Append("Province=@Province,");
			strSql.Append("City=@City,");
			strSql.Append("District=@District,");
			strSql.Append("Street=@Street,");
			strSql.Append("PostCode=@PostCode,");
			strSql.Append("DelFlag=@DelFlag,");
			strSql.Append("ParentId=@ParentId,");
			strSql.Append("depcode=@depcode");
			strSql.Append(" where DepID=@DepID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BranchID", MySqlDbType.Int32,11),
					new MySqlParameter("@DepName", MySqlDbType.VarChar,20),
					new MySqlParameter("@Manager", MySqlDbType.VarChar,20),
					new MySqlParameter("@Tel1", MySqlDbType.VarChar,20),
					new MySqlParameter("@Tel2", MySqlDbType.VarChar,20),
					new MySqlParameter("@Fax", MySqlDbType.VarChar,20),
					new MySqlParameter("@Province", MySqlDbType.VarChar,20),
					new MySqlParameter("@City", MySqlDbType.VarChar,20),
					new MySqlParameter("@District", MySqlDbType.VarChar,20),
					new MySqlParameter("@Street", MySqlDbType.VarChar,20),
					new MySqlParameter("@PostCode", MySqlDbType.VarChar,20),
					new MySqlParameter("@DelFlag", MySqlDbType.Int32,4),
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@depcode", MySqlDbType.VarChar,50),
					new MySqlParameter("@DepID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.BranchID;
			parameters[1].Value = model.DepName;
			parameters[2].Value = model.Manager;
			parameters[3].Value = model.Tel1;
			parameters[4].Value = model.Tel2;
			parameters[5].Value = model.Fax;
			parameters[6].Value = model.Province;
			parameters[7].Value = model.City;
			parameters[8].Value = model.District;
			parameters[9].Value = model.Street;
			parameters[10].Value = model.PostCode;
			parameters[11].Value = model.DelFlag;
			parameters[12].Value = model.ParentId;
			parameters[13].Value = model.depcode;
			parameters[14].Value = model.DepID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int DepID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from mrdepartment ");
			strSql.Append(" where DepID=@DepID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@DepID", MySqlDbType.Int32)
			};
			parameters[0].Value = DepID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string DepIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from mrdepartment ");
			strSql.Append(" where DepID in ("+DepIDlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.mrdepartment GetModel(int DepID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select DepID,BranchID,DepName,Manager,Tel1,Tel2,Fax,Province,City,District,Street,PostCode,DelFlag,ParentId,depcode from mrdepartment ");
			strSql.Append(" where DepID=@DepID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@DepID", MySqlDbType.Int32)
			};
			parameters[0].Value = DepID;

			hcrainMvc.Model.mrdepartment model=new hcrainMvc.Model.mrdepartment();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.mrdepartment DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.mrdepartment model=new hcrainMvc.Model.mrdepartment();
			if (row != null)
			{
				if(row["DepID"]!=null && row["DepID"].ToString()!="")
				{
					model.DepID=int.Parse(row["DepID"].ToString());
				}
				if(row["BranchID"]!=null && row["BranchID"].ToString()!="")
				{
					model.BranchID=int.Parse(row["BranchID"].ToString());
				}
				if(row["DepName"]!=null)
				{
					model.DepName=row["DepName"].ToString();
				}
				if(row["Manager"]!=null)
				{
					model.Manager=row["Manager"].ToString();
				}
				if(row["Tel1"]!=null)
				{
					model.Tel1=row["Tel1"].ToString();
				}
				if(row["Tel2"]!=null)
				{
					model.Tel2=row["Tel2"].ToString();
				}
				if(row["Fax"]!=null)
				{
					model.Fax=row["Fax"].ToString();
				}
				if(row["Province"]!=null)
				{
					model.Province=row["Province"].ToString();
				}
				if(row["City"]!=null)
				{
					model.City=row["City"].ToString();
				}
				if(row["District"]!=null)
				{
					model.District=row["District"].ToString();
				}
				if(row["Street"]!=null)
				{
					model.Street=row["Street"].ToString();
				}
				if(row["PostCode"]!=null)
				{
					model.PostCode=row["PostCode"].ToString();
				}
				if(row["DelFlag"]!=null && row["DelFlag"].ToString()!="")
				{
					model.DelFlag=int.Parse(row["DelFlag"].ToString());
				}
				if(row["ParentId"]!=null && row["ParentId"].ToString()!="")
				{
					model.ParentId=int.Parse(row["ParentId"].ToString());
				}
				if(row["depcode"]!=null)
				{
					model.depcode=row["depcode"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select DepID,BranchID,DepName,Manager,Tel1,Tel2,Fax,Province,City,District,Street,PostCode,DelFlag,ParentId,depcode ");
			strSql.Append(" FROM mrdepartment ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM mrdepartment ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.DepID desc");
			}
			strSql.Append(")AS Row, T.*  from mrdepartment T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "mrdepartment";
			parameters[1].Value = "DepID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

